package psn.kiko.service.business.associated;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import psn.kiko.constant.MessageConstant;
import psn.kiko.excepion.BaseBusinessException;
import psn.kiko.mapper.business.associated.OperationInterfaceMapper;
import psn.kiko.pojo.entity.system.OperationInterfaceEntity;
import psn.kiko.pojo.vo.PureInterfaceVO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作-接口服务
 */
@Service
public class OperationInterfaceService{
	@Resource
	OperationInterfaceMapper operationInterfaceMapper;
	
	/**
	 * 给操作关联接口列表
	 */
	public Integer allocateApisToOperation(List<Integer> interfaceStableIds,Integer operationId){
		ArrayList<OperationInterfaceEntity> list = new ArrayList<>(interfaceStableIds.size());
		for(Integer interfaceStableId: interfaceStableIds){
			list.add(new OperationInterfaceEntity(operationId,interfaceStableId));
		}
		try{
			return operationInterfaceMapper.insertBatch(list);
		}catch(DuplicateKeyException e){
			e.printStackTrace();
			throw new BaseBusinessException(MessageConstant.INSERT_DUPLICATE_ROWS);
		}
	}
	
	/**
	 * 删除指定操作关联的接口，interfaceStableIds为null或interfaceStableIds无有效元素，则删除操作关联的所有接口
	 * */
	public Integer unAllocateApisOfOperation(List<Integer> interfaceStableIds,Integer operationId){
		return operationInterfaceMapper.deleteBatch(operationId,interfaceStableIds);
	}
	
	/**
	 * 删除一批操作与接口的关联
	 * */
	public Integer unAllocateApisOfOperations(List<Integer> operationIds){
		return operationInterfaceMapper.deleteBatchByOperationId(operationIds);
	}
	
	public List<PureInterfaceVO> getInterfacesOfOperation(Integer operationId){
		return operationInterfaceMapper.selectInterfacesByOperationId(operationId);
	}
	
	public Integer deleteBatchByApiStableIds(List<Integer> stableIds){
		return operationInterfaceMapper.deleteByInterfaceId(stableIds);
	}
}
